﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;

namespace School.Pages.Teaching
{
    public partial class TE_Question_Bank_answer : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1504) == true)
                {
                    if (!this.Page.IsPostBack)
                    {
                        FillControls();
                    }
                    lbltst.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        private void FillControls()
        {
            ddlCampus.DataSource = School.Uniform.PayOrder.AllCampus();
            ddlCampus.DataBind();
        }

        private void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }
        protected void ddlCertificate_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlCertificate.SelectedValue != "0")
            {
                ddlStadge.DataSource = School.Uniform.PayOrder.GetStadgeByCertificate(int.Parse(ddlCertificate.SelectedValue));
                ddlBind(ddlStadge);
            }

        }
        protected void ddlStadge_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlStadge.SelectedValue != "0")
            {
                ddlGrade.DataSource = School.Uniform.PayOrder.GetGradeByStadge(int.Parse(ddlStadge.SelectedValue));
                ddlBind(ddlGrade);
            }

        }
        protected void ddlCampus_TextChanged(object sender, EventArgs e)
        {
            if (ddlCampus.SelectedValue != "0")
            {

                ddlCertificate.DataSource = School.Uniform.PayOrder.GetCertificateByCamp(int.Parse(ddlCampus.SelectedValue));
                ddlBind(ddlCertificate);
            }
        }

        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        protected void ddlGrade_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlGrade.SelectedIndex > 0)
            {
                ddlBind(ddlsubject, School.Teaching.Teaching.Get_Subject_By_Grad(Convert.ToInt32(ddlGrade.SelectedValue)));
            }
        }


        protected void ddlsubject_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlsubject.SelectedIndex > 0)
            {
                ddlBind(ddlQuestion, School.Teaching.Teaching.Get_Bank_Question_BySubj(Convert.ToInt32(ddlsubject.SelectedValue)));

            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate("a");
            if (Page.IsValid)
                if (txtanswer.Enabled)
                {
                    bool re = School.Teaching.Teaching.Add_Bank_Answer(Convert.ToInt32(ddlQuestion.SelectedValue.ToString()), txtanswer.Text);
                    if (re == true)
                    {
                        lbltst.ForeColor = Color.Green;
                        lbltst.Text = "تمت الاضافة بنجاح";
                        txtanswer.Text = "";
                        if (ddlQuestion.SelectedIndex > 0)
                        {
                            long Typ_Id = School.Teaching.Teaching.Get_Type_Bank_Id(Convert.ToInt32(ddlQuestion.SelectedValue));
                            if (Typ_Id == 2 || Typ_Id == 4)
                            {
                                plOnechoice.Visible = true;
                                plMulti.Visible = false;
                                // pltruefalse.Visible = false;
                                gvBank_ABind();
                                txtanswer.Enabled = true;

                            }
                            else if (Typ_Id == 3)
                            {
                                plOnechoice.Visible = false;
                                plMulti.Visible = true;
                                // pltruefalse.Visible = false;
                                gvBank_A_MultiBind();
                                txtanswer.Enabled = true;

                            }
                            //else
                            //{
                            //    plOnechoice.Visible = false;
                            //    plMulti.Visible = false;
                            //    // pltruefalse.Visible = true;
                            //    txtanswer.Enabled = false;

                            //}

                        }
                    }
                    else
                    {
                        lbltst.ForeColor = Color.Red;
                        lbltst.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
                    }
                    lbltst.Visible = true;
                }
        }
        private void gvBank_ABind()
        {
            gvBank_A.DataSource = School.Teaching.Teaching.Get_Bank_Answer_ByQues_Id(Convert.ToInt32(ddlQuestion.SelectedValue));
            gvBank_A.DataBind();
        }
        private void gvBank_A_MultiBind()
        {
            gvMultiChoice.DataSource = School.Teaching.Teaching.Get_Bank_Answer_ByQues_Id(Convert.ToInt32(ddlQuestion.SelectedValue));
            gvMultiChoice.DataBind();
        }
        protected void gvBank_A_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {

                foreach (GridViewRow row in gvBank_A.Rows)
                {
                    RadioButton ch = (RadioButton)row.FindControl("chcoice");
                    //Get_type_answer
                    int id = Convert.ToInt32(gvBank_A.DataKeys[row.RowIndex].Value.ToString());
                    int type = School.Teaching.Teaching.Get_type_answer(Convert.ToInt32(ddlQuestion.SelectedValue), id);

                    if (type == 1)
                        ch.Checked = true;
                    //else
                    //    ch.Checked = false;

                }

                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }

        protected void save_Click(object sender, EventArgs e)
        {

            bool re = false;
            foreach (GridViewRow Row in gvBank_A.Rows)
            {
                RadioButton ch = (RadioButton)Row.FindControl("chcoice");
                int id = Convert.ToInt32(gvBank_A.DataKeys[Row.RowIndex].Value.ToString());

                if (ch.Checked)
                {
                    re = School.Teaching.Teaching.UpdateBank_Answer_Choice_Type(id, 1);

                }
                else
                {
                    re = School.Teaching.Teaching.UpdateBank_Answer_Choice_Type(id, 2);
                }

            }
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
                gvBank_ABind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvBank_A_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvBank_A.PageIndex = e.NewPageIndex;
            gvBank_ABind();
        }

        protected void gvBank_A_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvBank_A.EditIndex = -1;
            gvBank_ABind();
        }

        protected void gvBank_A_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvBank_A.EditIndex = e.NewEditIndex;
            gvBank_ABind();
        }

        protected void gvBank_A_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvBank_A.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Teaching.Teaching.DeleteBank_Answer(Id);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم الحذف بنجاح";
                gvBank_ABind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvBank_A_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvBank_A.DataKeys[e.RowIndex].Value.ToString());
            TextBox txt_answ_E = (TextBox)gvBank_A.Rows[e.RowIndex].FindControl("txt_answ_E");


            bool re = School.Teaching.Teaching.UpdateBank_Answer(Id, txt_answ_E.Text);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
                gvBank_A.EditIndex = -1;
                gvBank_ABind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void ddlQuestion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlQuestion.SelectedIndex > 0)
            {
                //Get_Type_Bank_Id
                long Typ_Id = School.Teaching.Teaching.Get_Type_Bank_Id(Convert.ToInt32(ddlQuestion.SelectedValue));
                if (Typ_Id == 2 || Typ_Id == 4)
                {
                    plOnechoice.Visible = true;
                    plMulti.Visible = false;
                    //pltruefalse.Visible = false;
                    gvBank_ABind();
                    txtanswer.Enabled = true;
                    btnSave.Visible = true;

                }
                else if (Typ_Id == 3)
                {
                    plOnechoice.Visible = false;
                    plMulti.Visible = true;
                    // pltruefalse.Visible = false;
                    gvBank_A_MultiBind();
                    txtanswer.Enabled = true;
                    btnSave.Visible = true;
                }
                //else
                //{
                //    plOnechoice.Visible = false;
                //    plMulti.Visible = false;
                //    pltruefalse.Visible = true;
                //    txtanswer.Enabled = false;
                //    btnSave.Visible = false;

                //}

            }
        }

        protected void gvBank_A_RowCommand(object sender, GridViewCommandEventArgs e)
        {




            //GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
            //RadioButton rbTemp = (RadioButton)gvBank_A.Rows[row.RowIndex].FindControl("chcoice");
            //ClientScript.RegisterStartupScript(this.GetType(), "HighlightRow", "<script type=text/javascript>CheckOtherIsCheckedByGVIDMore(document.getElementById('" + rbTemp.ClientID + "'))</script>");
        }

        protected void save_Multi_Click(object sender, EventArgs e)
        {

            bool re = false;
            foreach (GridViewRow Row in gvMultiChoice.Rows)
            {
                CheckBox ch = (CheckBox)Row.FindControl("chcoice");
                int id = Convert.ToInt32(gvMultiChoice.DataKeys[Row.RowIndex].Value.ToString());

                if (ch.Checked)
                {
                    re = School.Teaching.Teaching.UpdateBank_Answer_Choice_Type(id, 1);

                }
                else
                {
                    re = School.Teaching.Teaching.UpdateBank_Answer_Choice_Type(id, 2);
                }

            }
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
                gvBank_A_MultiBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        //protected void btntrufalseSave_Click(object sender, EventArgs e)
        //{
        //    Page.Validate("s");
        //    if (Page.IsValid)
        //    {
        //        bool re = false;
        //        int id = Convert.ToInt32(rdbtruefalse.SelectedValue);
        //        int count = School.Teaching.Teaching.Get_From_Answer(Convert.ToInt32(ddlQuestion.SelectedValue));
        //        if (count > 0)
        //        {
        //            var query = School.Teaching.Teaching.Get_Bank_Answer_ByQues_Id(Convert.ToInt32(ddlQuestion.SelectedValue));
        //            foreach (var item in query.ToList())
        //            {
        //                re = School.Teaching.Teaching.DeleteBank_Answer_T_F(Convert.ToInt32(ddlQuestion.SelectedValue));
        //            }

        //            if (re)
        //                re = School.Teaching.Teaching.Add_Bank_Answer(Convert.ToInt32(ddlQuestion.SelectedValue), rdbtruefalse.SelectedItem.Text, id);
        //        }
        //        else
        //        {
        //            re = School.Teaching.Teaching.Add_Bank_Answer(Convert.ToInt32(ddlQuestion.SelectedValue), rdbtruefalse.SelectedItem.Text, id);
        //        }
        //        if (re == true)
        //        {
        //            lbltst.ForeColor = Color.Green;
        //            lbltst.Text = "تم التحديث بنجاح";

        //        }
        //        else
        //        {
        //            lbltst.ForeColor = Color.Red;
        //            lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
        //        }
        //        lbltst.Visible = true;
        //    }
        //}

        protected void gvMultiChoice_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvMultiChoice.PageIndex = e.NewPageIndex;
            gvBank_A_MultiBind();
        }

        protected void gvMultiChoice_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvMultiChoice.EditIndex = -1;
            gvBank_A_MultiBind();
        }

        protected void gvMultiChoice_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvMultiChoice.EditIndex = e.NewEditIndex;
            gvBank_A_MultiBind();
        }

        protected void gvMultiChoice_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvMultiChoice.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Teaching.Teaching.DeleteBank_Answer(Id);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم الحذف بنجاح";
                gvBank_A_MultiBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

        protected void gvMultiChoice_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvMultiChoice.DataKeys[e.RowIndex].Value.ToString());
            TextBox txt_answ_E = (TextBox)gvMultiChoice.Rows[e.RowIndex].FindControl("txt_answ_E");


            bool re = School.Teaching.Teaching.UpdateBank_Answer(Id, txt_answ_E.Text);
            if (re == true)
            {
                lbltst.ForeColor = Color.Green;
                lbltst.Text = "تم التحديث بنجاح";
                gvMultiChoice.EditIndex = -1;
                gvBank_A_MultiBind();
            }
            else
            {
                lbltst.ForeColor = Color.Red;
                lbltst.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltst.Visible = true;
        }

    }
}